package com.ak.game.service.impl;

import com.ak.game.entity.PnGame;
import com.ak.game.mapper.PnGameMapper;
import com.ak.game.service.PnGameService;
import com.ak.game.util.Md5SignatureUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 游戏信息表(PnGame)表服务实现类
 *
 * @author makejava
 * @since 2024-05-20 19:19:42
 */
@Service
public class PnGameServiceImpl extends ServiceImpl<PnGameMapper, PnGame> implements PnGameService {

    @Autowired
    private PnGameMapper gameMapper;

    @Value("${client-sign}")
    private String sign;

    @Value("${game.code}")
    private String gameCode;

    @Value("${game.id}")
    private String gameId;

    /**
     * 验证sign签名的service方法
     *
     * @param sign      md5加密后的sign
     * @param gameId    游戏id
     * @param timestamp 时间戳
     * @return
     */
    @Override
    public boolean validSignature(String sign, String gameId, Long timestamp) {
        // 根据游戏name查询pn_game表里的数据,看看这个游戏id是否存在
        LambdaQueryWrapper<PnGame> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PnGame::getId, gameId);
        queryWrapper.eq(PnGame::getGameCode, gameCode);
        if (gameMapper.exists(queryWrapper)) {
            // sign加密并进行比对
            return Md5SignatureUtils.generateMd5Sign(String.format("%s%s%d", this.sign, gameId, timestamp)).equals(sign);
        }
        return false;
    }

    @Override
    public PnGame getPnGame() {
        LambdaQueryWrapper<PnGame> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PnGame::getId, gameId);
        return this.getOne(queryWrapper);
    }
}

